package com.kehao.leetcode.basic.dp;

import org.junit.Test;

public class DPSolution3 {

    public int maxSubArray(int[] nums) {
//        int[] answer = new int[nums.length];
//        answer[0] = nums[0];
//        for (int i = 1; i < answer.length; i++) {
//            answer[i] = Math.max(0,answer[i-1])+nums[i];
//        }
//        int max = Integer.MIN_VALUE;
//        for (int i = 0; i < answer.length; i++) {
//            max = max<answer[i]?answer[i]:max;
//        }
//        return max;

        int answer = Integer.MIN_VALUE;
        int max = nums[0];
        answer = nums[0];
        for (int i = 1; i < nums.length; i++) {
            answer = Math.max(0,answer)+nums[i];
            max = Math.max(answer,max);
        }
        return max;
    }

    @Test
    public void test01(){
        System.out.println(maxSubArray(new int[]{1}));
    }
}
